# -*- coding: utf-8 -*-
from common import orm
from common.lottery.cyclical import ACTIVITY_STATUS


class Activity(orm.Model):
    __tablename__ = "js_ks"
    id = orm.Column(orm.Integer, primary_key=True)
    term = orm.Column(orm.VARCHAR)          # 期号
    number = orm.Column(orm.VARCHAR)        # 开奖结果
    reference = orm.Column(orm.TEXT)        # 爬取结果页url
    status = orm.Column(orm.SmallInteger)       # 状态
    start_ts = orm.Column(orm.Integer)      # 我方开始时间戳（整点）
    end_ts = orm.Column(orm.Integer)        # 我方结束时间戳（略早于官方）
    announce_ts = orm.Column(orm.Integer)   # 爬取到结果的时间戳
    created_at = orm.Column(orm.DATETIME)
    updated_at = orm.Column(orm.DATETIME)


class Trend(orm.Model):
    ''' 只有3位，由于这个结果是无序的，所以idx其实没什么意义
    '''
    __tablename__ = "js_ks_trend"
    id = orm.Column(orm.Integer, primary_key=True)
    term = orm.Column(orm.VARCHAR)
    number = orm.Column(orm.VARCHAR)
    idx_0 = orm.Column(orm.VARCHAR)  # 百位
    idx_1 = orm.Column(orm.VARCHAR)  # 十位
    idx_2 = orm.Column(orm.VARCHAR)  # 个位
    miss_1 = orm.Column(orm.TEXT)
    miss_2 = orm.Column(orm.TEXT)
    miss_4 = orm.Column(orm.TEXT)
    miss_5 = orm.Column(orm.TEXT)
    miss_6 = orm.Column(orm.TEXT)
    miss_8 = orm.Column(orm.TEXT)
    miss_9 = orm.Column(orm.TEXT)

    @classmethod
    def get_defaults(cls):
        data = {}
        for k in range(3):
            data['idx_%s' % k] = ','.join('x' * 6)
        data['miss_1'] = ','.join('x' * 16)
        data['miss_2'] = ','.join('x' * 6)
        data['miss_4'] = '|'.join([','.join('x' * 6)] * 2)
        data['miss_5'] = ','.join('x' * 6)
        data['miss_6'] = ','.join('x' * 6)
        data['miss_8'] = 'x'
        data['miss_9'] = ','.join('x' * 6)

        return data

    def get_stat(self, bet_type):
        ''' 根据玩法返回统计（遗漏、冷热、最大和上次）数据
            这个数据应该被缓存起来（因为冷热和最大需要实时计算）
            对于每一期而言，这个数据是固定的
        '''
        resp = {}
        if bet_type in {1, 2, 4, 5, 6, 8, 9}:
            resp['miss'] = getattr(self, 'miss_%s' % bet_type)
        elif bet_type == 3:
            resp['miss'] = self.miss_2.split('|')[0]
        elif bet_type == 7:
            resp['miss'] = self.miss_6

        return resp
